package org.geektimes.projects.user.validation.impl;

import org.apache.commons.lang.StringUtils;
import org.geektimes.projects.user.validation.annotation.PhoneNumber;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author : zghuang
 * @date : 2021/3/15 17:19
 */
public class PhoneNumberConstraintValidator implements ConstraintValidator<PhoneNumber,String> {
    public static final Pattern CHINA_pattern = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");
    @Override
    public void initialize(PhoneNumber phoneNumber) {

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.isNotBlank(s)){
            return CHINA_pattern.matcher(s).matches();
        }
        return false;
    }
}
